<?php
// @formatter:off
/**
 * @file t.comic.clean.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: t.comic.clean.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/tools/t.comic.clean.php $
 */

$wcToolScripts["comic.clean"]	= array(
	"name"		=> "Comic Clean",
	"description"	=> "Clears all recoverable data of a comic",
	"class"		=> "WCToolComicClean",
	"url"		=> "http://code.google.com/p/wcomix/",
	"enabled"	=> true
);
// @formatter:on

class WCToolComicClean extends WCTool {
	public function run($spacer = "") {
		global $wcDefaults;
		global $wcOptions;

		if(!$wcDefaults["read-only"] && $wcOptions->activated("Comic") && $wcOptions->activated("Group")) {
			global $wcComicsHolder;
			global $wcGroupsHolder;
			global $wcTagsHolder;
			global $wcComic;
			global $wcDirectory;
			global $wcGroup;
			global $wcCollection;

			$isTest = $wcOptions->activated("Test");
			$comicId = $wcOptions->value("Comic");
			$groupId = $wcOptions->value("Group");

			$ok = true;

			//
			// Checking group.
			if($ok) {
				$wcGroup = $wcGroupsHolder->groupById($groupId);

				if(!$wcGroup || !$wcGroup->ok()) {
					$ok = false;
					echo $spacer.WCColorText::ShellRed("Failed").": Group id '{$groupId}' is not valid.\n";
				}
			}
			//
			// Checking comic.
			if($ok) {
				$wcComic = $wcComicsHolder->item($comicId, $wcGroup);

				if(!$wcComic) {
					$ok = false;
					echo $spacer.WCColorText::ShellRed("Failed").": Comic id '{$comicId}' is not valid.\n";
				} elseif($wcComic->lost) {
					$ok = false;
					echo $spacer.WCColorText::ShellRed("Failed").": Comic id '{$wcComic->name()}' is lost.\n";
				} elseif(!$wcComic->ok()) {
					$ok = false;
					echo $spacer.WCColorText::ShellRed("Failed").": Comic id '{$wcComic->name()}' is not ok.\n";
				} else {
					$wcDirectory = $wcComic->directory();
					$wcCollection = $wcComic->collection();
				}
			}
			//
			// Clearing comic.
			if($ok) {
				//
				// Information.
				echo "{$spacer}Clearing comic '{$wcComic->name()}' ({$wcComic->id()}@{$wcDirectory->id()}):\n";
				echo "{$spacer}\tGroup:\t\t'{$wcGroup->name()}' ({$wcGroup->id()})\n";
				echo "{$spacer}\t\tRoot:\t'{$wcGroup->root()}'\n";
				echo "{$spacer}\tDirectory:\t'{$wcDirectory->name()}' ({$wcDirectory->id()})\n";
				echo "{$spacer}\tExpand Path:\t'{$wcComic->expandedPath()}'\n";
				echo "{$spacer}\tThumb Path:\t'{$wcComic->previewPath()}'\n";
				echo "{$spacer}\tThumbs Path:\t'{$wcComic->previewDirPath()}'\n";
				echo "{$spacer}\tTags:\t\t".count($wcTagsHolder->tagsOf($wcComic))."\n";
				if($wcCollection) {
					echo "{$spacer}\tCollection:\t'{$wcCollection->name()}'\n";
				}
				echo "\n";
				//
				// Removing expanded items
				if($wcComic->isExpanded()) {
					echo "{$spacer}Removing directory '{$wcComic->expandedPath()}'";

					if($isTest) {
						echo ": ".WCColorText::ShellGreen("Done")." (simulation)\n";
					} else {
						$results = recursive_unlink($wcComic->expandedPath());

						if(!count($results)) {
							echo ": ".WCColorText::ShellGreen("Done")."\n";
						} else {
							echo ": ".WCColorText::ShellRed("Failed")."\n";
							foreach($result as $issue) {
								echo "{$spacer}\t- {$issue}\n";
							}
						}
					}

				}
				//
				// Removing thumb
				if(is_readable($wcComic->previewPath())) {
					echo "{$spacer}Removing thumb '{$wcComic->previewPath()}'";

					if(is_writable($wcComic->previewPath())) {
						if($isTest) {
							echo ": ".WCColorText::ShellGreen("Done")." (simulation)\n";
						} else {
							$results = recursive_unlink($wcComic->previewPath());

							if(!count($results)) {
								echo ": ".WCColorText::ShellGreen("Done")."\n";
							} else {
								echo ": ".WCColorText::ShellRed("Failed")."\n";
								foreach($result as $issue) {
									echo "{$spacer}\t- {$issue}\n";
								}
							}
						}
					} elseif(!is_writable($wcComic->previewPath())) {
						echo ": ".WCColorText::ShellRed("Failed")."\n";
						echo "{$spacer}\t- Your have no permissions to write this path.\n";
					}
				}
				//
				// Removing thumbs directory
				if(is_readable($wcComic->previewDirPath())) {
					echo "{$spacer}Removing thumb '{$wcComic->previewDirPath()}'";

					if(is_dir($wcComic->previewDirPath()) && is_writable($wcComic->previewDirPath())) {
						if($isTest) {
							echo ": ".WCColorText::ShellGreen("Done")." (simulation)\n";
						} else {
							$results = recursive_unlink($wcComic->previewDirPath());

							if(!count($results)) {
								echo ": ".WCColorText::ShellGreen("Done")."\n";
							} else {
								echo ": ".WCColorText::ShellRed("Failed")."\n";
								foreach($result as $issue) {
									echo "{$spacer}\t- {$issue}\n";
								}
							}
						}
					} elseif(!is_writable($wcComic->previewPath())) {
						echo ": ".WCColorText::ShellRed("Failed")."\n";
						echo "{$spacer}\t- Your have no permissions to write this path.\n";
					} else {
						echo ": ".WCColorText::ShellRed("Failed")."\n";
						echo "{$spacer}\t- Path doesn't seem to be a directory.\n";
					}
				}
				//
				// Setting comic flags.
				// @{
				echo "{$spacer}Setting comic as not checked";
				if($isTest) {
					echo ": ".WCColorText::ShellGreen("Done")." (simulation)\n";
				} else {
					$wcComic->checked = false;
					echo ": ".WCColorText::ShellGreen("Done")."\n";
				}
				echo "{$spacer}Setting comic as not indexed";
				if($isTest) {
					echo ": ".WCColorText::ShellGreen("Done")." (simulation)\n";
				} else {
					$wcComic->indexed = false;
					echo ": ".WCColorText::ShellGreen("Done")."\n";
				}
				echo "{$spacer}Setting comic as not generated";
				if($isTest) {
					echo ": ".WCColorText::ShellGreen("Done")." (simulation)\n";
				} else {
					$wcComic->generated = false;
					echo ": ".WCColorText::ShellGreen("Done")."\n";
				}
				echo "{$spacer}Clearing comic paths and extensions:\n";
				echo "{$spacer}\t- Expanded Path";
				if($isTest) {
					echo ": ".WCColorText::ShellGreen("Done")." (simulation)\n";
				} else {
					$wcComic->expanded = "";
					echo ": ".WCColorText::ShellGreen("Done")."\n";
				}
				echo "{$spacer}\t- Thumbs Path";
				if($isTest) {
					echo ": ".WCColorText::ShellGreen("Done")." (simulation)\n";
				} else {
					$wcComic->thumbs = "";
					echo ": ".WCColorText::ShellGreen("Done")."\n";
				}
				echo "{$spacer}\t- Thumbs Extension";
				if($isTest) {
					echo ": ".WCColorText::ShellGreen("Done")." (simulation)\n";
				} else {
					$wcComic->thumbs_ext = "";
					echo ": ".WCColorText::ShellGreen("Done")."\n";
				}
				// @}
				//
				// Invalidating tags previews.
				echo "{$spacer}Invalidating comic tags' previews:\n";
				foreach($wcTagsHolder->tagsOf($wcComic) as $tag) {
					echo "{$spacer}\t- '{$tag->name}' ({$tag->id})";
					if($isTest) {
						echo ": ".WCColorText::ShellGreen("Done")." (simulation)\n";
					} else {
						$tag->valid_preview = false;
						echo ": ".WCColorText::ShellGreen("Done")."\n";
					}
				}
				//
				// Invalidating collection previews.
				if($wcCollection) {
					echo "{$spacer}Invalidating collection '{$wcCollection->name()}' previews";
					if($isTest) {
						echo ": ".WCColorText::ShellGreen("Done")." (simulation)\n";
					} else {
						$wcCollection->valid_preview = false;
						echo ": ".WCColorText::ShellGreen("Done")."\n";
					}
				}
			}
		} elseif(!$wcOptions->activated("Comic")) {
			echo $spacer.WCColorText::ShellRed("Failed").": No comic id specified.\n";
		} elseif(!$wcOptions->activated("Group")) {
			echo $spacer.WCColorText::ShellRed("Failed").": No group id specified.\n";
		} elseif($wcDefaults["read-only"]) {
			echo $spacer.WCColorText::ShellYellow("ReadOnlyMode activated.\n");
		} else {
			$this->showHelp($spacer);
		}
	}
	public function showHelp($spacer = "") {
		echo "{$spacer}Clears all recoverable data of a comic such as:\n";
		echo "{$spacer}\t- thumnails,\n";
		echo "{$spacer}\t- search data,\n";
		echo "{$spacer}\t- expanded directories.\n\n";
		echo "{$spacer}Options:\n";
		echo "{$spacer}\t-c comic-id, --comic comic-id\n";
		echo "{$spacer}\t\tValid comic id to clean.\n\n";
		echo "{$spacer}\t-g group-id, --group group-id\n";
		echo "{$spacer}\t\tGroup in which the comic to clear is held.\n\n";
		echo "{$spacer}\t-t, --test\n";
		echo "{$spacer}\t\tShows all the actions it may take, but performs none. This\n";
		echo "{$spacer}\t\tis the simulated method.\n";
	}
	protected function loadOptions() {
		if(!$this->_optionsLoaded) {
			global $wcOptions;

			if($wcOptions->addValueOption("Comic")) {
				$wcOptions->addOptionTrigger("Comic", "c");
				$wcOptions->addOptionTrigger("Comic", "comic");
			}
			if($wcOptions->addValueOption("Group")) {
				$wcOptions->addOptionTrigger("Group", "g");
				$wcOptions->addOptionTrigger("Group", "group");
			}
			if($wcOptions->addOption("Test")) {
				$wcOptions->addOptionTrigger("Test", "t");
				$wcOptions->addOptionTrigger("Test", "test");
			}

			$wcOptions->check();

			$this->_optionsLoaded = true;
		}
	}
}
?>